package com.bawei.goods.hospital_goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.bawei.common.utils.R;
import com.bawei.goods.hospital_goods.dao.GoodTableDao;
import com.bawei.goods.hospital_goods.entity.GoodTableEntity;
import com.bawei.goods.hospital_goods.service.GoodTableService;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bawei.common.utils.PageUtils;
import com.bawei.common.utils.Query;

import com.bawei.goods.hospital_goods.dao.SeckillListDao;
import com.bawei.goods.hospital_goods.entity.SeckillListEntity;
import com.bawei.goods.hospital_goods.service.SeckillListService;


@Service("seckillListService")
public class SeckillListServiceImpl extends ServiceImpl<SeckillListDao, SeckillListEntity> implements SeckillListService {

    @Autowired
    private GoodTableService goodTableService;





    @Override
    public PageUtils queryPage(Map<String, Object> params) {


        String status = (String) params.get("status");
        String key = (String) params.get("key");
        //已提交的秒杀
        if (status.equals("2")){

            QueryWrapper<SeckillListEntity> wrapper = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(key)){
                wrapper.like("good_name", key);
            }
            IPage<SeckillListEntity> page = this.page(
                    new Query<SeckillListEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
            //未提交的秒杀
        }else {
            QueryWrapper<GoodTableEntity> wrapper = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(key)){
                wrapper.like("good_name", key);
            }
            //排序
            wrapper.orderByAsc("sort");
            IPage<GoodTableEntity> page = goodTableService.page(
                    new Query<GoodTableEntity>().getPage(params),
                    wrapper
            );

            List<SeckillListEntity> list1 =page.getRecords().stream().map(item -> {
                SeckillListEntity seckillListEntity = new SeckillListEntity();
                seckillListEntity.setGoodId(item.getGoodNum());
                seckillListEntity.setGoodName(item.getGoodName());
                seckillListEntity.setGoodImg(item.getGoodImg());
                seckillListEntity.setGoodPrice(item.getGoodPrice());
                return seckillListEntity;
            }).collect(Collectors.toList());

            return new PageUtils(list1, (int) page.getTotal(), (int) page.getSize(),(int)  page.getCurrent());
        }

        //todo从redis中取




    }

    @Override
    public R getGood() {
        //当前时间
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        LocalDateTime of = LocalDateTime.of(now, min);
        String format = of.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        //结束时间
        LocalDate localDate = now.plusDays(1);
        LocalTime max = LocalTime.MAX;
        LocalDateTime of1 = LocalDateTime.of(localDate, max);
        String format1 = of1.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        QueryWrapper<SeckillListEntity> wrapper = new QueryWrapper<>();
        wrapper.between("seckill_starttime", format,format1);
        List<SeckillListEntity> list = this.list(wrapper);


        if (list.size()>=1){
            return R.ok().setData(list);
        }
        return R.error();

    }


}